using SmartCommunity.Models;
using Mapster;
using System.ComponentModel.DataAnnotations;

namespace SmartCommunity.WebApi.Services.System
{
    #region DTO属性映射
    /// <summary>
    /// 属性映射 LogDtoRegister
    /// </summary>
    public class LogDtoRegister : IRegister
    {
        public void Register(TypeAdapterConfig config)
        {
            //config.ForType<LogEntity, LogDto>().Map(d => d.DeptName, s => s.Dept.DeptName, s => s.Dept != null);
        }
    }

    #endregion

    #region 数据传输对象 LogDto
    /// <summary>
    /// 数据传输对象：操作日志
    /// </summary>
    public class LogDto : BaseDto
    {
        /// <summary>
        /// 日志ID
        /// </summary>
        public long LogId { get; set; }

        /// <summary>
        /// 操作类型
        /// </summary>
        public int OperateType { get; set; }

        /// <summary>
        /// 日志类别(简短描述)
        /// </summary>
        [Required(ErrorMessage = "日志类别(简短描述)不能为空")]
        [StringLength(50, ErrorMessage = "日志类别(简短描述) 长度不能超过 50.")]
        public string LogTitle { get; set; } = string.Empty;

        /// <summary>
        /// 日志内容
        /// </summary>
        [Required(ErrorMessage = "日志内容不能为空")]
        [StringLength(2000, ErrorMessage = "日志内容 长度不能超过 2000.")]
        public string LogContent { get; set; } = string.Empty;

        /// <summary>
        /// 请求URL
        /// </summary>
        [StringLength(200, ErrorMessage = "请求URL 长度不能超过 200.")]
        public string? RequestPath { get; set; }

        /// <summary>
        /// 请求方式
        /// </summary>
        [StringLength(10, ErrorMessage = "请求方式 长度不能超过 10.")]
        public string? RequestMethod { get; set; }

        /// <summary>
        /// 请求参数
        /// </summary>
        [StringLength(2000, ErrorMessage = "请求参数 长度不能超过 2000.")]
        public string? RequestParam { get; set; }

        /// <summary>
        /// 是否执行成功
        /// </summary>
        public bool Success { get; set; }

        /// <summary>
        /// 执行时长,毫秒
        /// </summary>
        public int ExecuteTime { get; set; }

        /// <summary>
        /// 客户端IP
        /// </summary>
        [StringLength(20, ErrorMessage = "客户端IP 长度不能超过 20.")]
        public string? ClientIp { get; set; }

        /// <summary>
        /// 客户端操作系统
        /// </summary>
        [StringLength(50, ErrorMessage = "客户端操作系统 长度不能超过 50.")]
        public string? ClientOs { get; set; }

        /// <summary>
        /// 客户端浏览器
        /// </summary>
        [StringLength(50, ErrorMessage = "客户端浏览器 长度不能超过 50.")]
        public string? ClientBrowser { get; set; }
    }
    #endregion

    #region 数据查询对象
    /// <summary>
    /// 查询模型：操作日志
    /// </summary>
    public class LogQuery : AbstractQuery<LogEntity>
    {
        /// <summary>
        /// 主键ID多选查询
        /// </summary>
        public IEnumerable<long> Ids { get; set; } = [];

        /// <summary>
        /// 是否执行成功
        /// </summary>
        public bool? Success { get; set; }

        /// <summary>
        /// 操作类型
        /// </summary>
        public short? OperateType { get; set; }

        /// <summary>
        /// 根据查询条件添加查询表达式
        /// </summary>
        protected override void MergeQueryExpression()
        {
            AddExpression(Ids.Any(), e => Ids.Contains(e.LogId));
            AddExpression(OperateType.HasValue, r => ((int)r.OperateType) == OperateType!.Value);
            AddExpression(Success.HasValue, r => r.Success == Success!.Value);
            AddSearchExpression(s => o => o.LogTitle.Contains(s) || o.CreatedBy.Contains(s));
            AddDateExpression(start => e => e.CreatedTime >= start, end => e => e.CreatedTime <= end);
        }
    }
    #endregion
}
